
import request from '@/utils/request'


export default {
  namespaced: true,
  state: {
    goodList:[],
    shopName:'',
    createBy:'' //商店创建人的ID
  },
  mutations: {
      SET_GOODLIST(state,goodList){
          var shopName  = sessionStorage.getItem("shopName");
          var username = sessionStorage.getItem("username");
          state.goodList = sessionStorage.getItem(shopName+username+"goodList") ?JSON.parse(sessionStorage.getItem(shopName+username+"goodList"))  : goodList;
      },
      UPDATE_GOODLIST(state,food){
        state.goodList.some(item => {
          if(item.id == food.foodCategoryId){
            if(isNaN(item.num)){
              item.num = 0;
            }
            
            if(food.type == 1)
              item.num++;
            else
              item.num--;
            return true;
          }
        })
      }
  },
  actions: {
        async getGoodList({commit,state},callback){
          await request.get('/schoolOrder/app/food/findAllFoodByCreateBy',{
            params:{
                createBy: state.createBy ? state.createBy : sessionStorage.getItem('createBy')
              }
          }).then(
            res => {
              commit('SET_GOODLIST',res.data.data)
              callback && callback()
            }
          )
        }
  },

}
